package com.tom.zx.service.impl;

import com.tom.zx.entity.HouseInformation;
import com.tom.zx.entity.HousePictureInformation;
import com.tom.zx.mapper.HouseInformationMapper;
import com.tom.zx.service.IHouseInformationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tom.zx.service.IHousePictureInformationService;
import com.tom.zx.vo.HouseInformationVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 房屋信息 服务实现类
 *
 * @author lp
 * @since 2022-04-09
 */
@Service
public class HouseInformationServiceImpl
    extends ServiceImpl<HouseInformationMapper, HouseInformation>
    implements IHouseInformationService {

  @Autowired public IHousePictureInformationService housePictureInformationService;

  @Override
  @Transactional(rollbackFor = Exception.class)
  public boolean saveOrUpdate(HouseInformationVo houseInformationVo) {
    final HouseInformation houseInformation = new HouseInformation();
    BeanUtils.copyProperties(houseInformationVo, houseInformation);
    saveOrUpdate(houseInformation);
    List<HousePictureInformation> housePictureInformationList =
        houseInformationVo.getHousePictureInformationList();
    housePictureInformationList =
        housePictureInformationList.stream()
            .peek(
                housePictureInformation ->
                    housePictureInformation.setHouseInformationId(houseInformation.getId()))
            .collect(Collectors.toList());
    return housePictureInformationService.saveOrUpdateBatch(housePictureInformationList);
  }
}
